/**
 * Copyright (C) 2013 The yydorid-framework project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.yangying.androidframework.activity;

import net.yangying.androidframework.core.view.TitleBarLayout;
import net.yangying.androidframework.util.Constants;
import net.yangying.androidframework.util.ViewUtil;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.RelativeLayout;


public abstract class AbstractTitleActivity extends BaseActivity implements View.OnClickListener {
	
	private static final String TAG = AbstractTitleActivity.class.getSimpleName();
	
	public TitleBarLayout tbl;
	protected RelativeLayout titleBar;
	public static final int TITLE_BTN_LEFT = 0;
	public static final int TITLE_BTN_RIGHT = 1;
	public static final int TITLE_BTN_SEARCH = 2;

	protected void onCreate(Bundle paramBundle) {
		super.onCreate(paramBundle);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		//unavailable ,must be set MainTabActivity
//        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
		loadData();
	}
	
	protected abstract void handleTitleBarEvent(int paramInt);

	public void onClick(View paramView) {
		if (paramView == this.tbl.leftButton) {
			handleTitleBarEvent(TITLE_BTN_LEFT);
		} else if (paramView == this.tbl.rightButton) {
			handleTitleBarEvent(TITLE_BTN_RIGHT);
		} else if(paramView == this.tbl.searchButton) {
			handleTitleBarEvent(TITLE_BTN_SEARCH);
		}
	}

	protected void setTitleBar(int type, String left_btntext, String title,
			String right_btntext) {
		if (this.tbl != null)
			this.tbl.setButtonTypeAndInfo(type, left_btntext, title,
					right_btntext);
	}
	protected void setTitleBar(int type, String title) {
		if (this.tbl != null)
			this.tbl.setButtonTypeAndInfo(type, "", title, "");
	}

	protected void setTitleBar(String title){
		if(title.length() <= ViewUtil.TITLE_MAX_NUM){
			tbl.tvInfo.setText(title);	
		} else {
			tbl.tvInfo.setText(title.substring(0, ViewUtil.TITLE_MAX_NUM) + Constants.ELLIPSIS);
		}
		
	}
	protected void setView(int layoutId) {
		this.tbl = new TitleBarLayout(this, layoutId);
		setContentView(this.tbl);
		initUI();
		initBaseListener();
		initListener();
	}
	
	private void initBaseListener(){
		this.tbl.leftButton.setOnClickListener(this);
		this.tbl.rightButton.setOnClickListener(this);
		this.tbl.searchButton.setOnClickListener(this);
	}

	public void setBaseTitle(String title){
	    this.tbl.tvInfo.setText(title);
	}
	
	protected void setTitleProgressVisibility(boolean flag){
	    this.tbl.setTitleProgressVisibility(flag);
	}
	
	protected void setTitleSearchBar(boolean flag){
		this.tbl.setTitleSearchBarVisibility(flag);
	}
	
	protected void setTitleSearchBarIME(int imeOptions){
		this.tbl.setTitleSearchBarIME(imeOptions);
	}

	protected void setTitleSearchBarEditTextHint(String hint){
		this.tbl.setTitleSearchBarEditTextHint(hint);
	}
	
	protected void setOnSearchBarListener(Context context){
		this.tbl.setOnSearchBarListener(context);
	}
	
	protected void setTitleSearchBarEditText(String text){
		this.tbl.setTitleSearchBarEditText(text);
	}
	
	protected EditText getSearchBarEditText(){
		return this.tbl.getSearchBarEditText();
	}
}
